package com.mzydz.shutdown;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import java.util.Calendar;

public class ShutdownService extends Service {
    public ShutdownService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getStringExtra("action");
        createAlarm();
        if("start".equals(action)){

        }else if("end".equals(action)){
            Intent iAct = new Intent(ShutdownService.this, ShutdownActivity.class);
            iAct.putExtra("action", "shutdown");
            startActivity(iAct);
        }
        Log.d("ShutdownService", "onStartCommand:" + action);
        Toast.makeText(ShutdownService.this, action, Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent, flags, startId);
    }

    private void createAlarm(){
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

        SharedPreferences sp =  getSharedPreferences("alarm", Activity.MODE_PRIVATE);
        int startHour = sp.getInt("startHour", 0);
        int startMinute = sp.getInt("startMinute", 0);
        int endHour = sp.getInt("endHour", 0);
        int endMinute = sp.getInt("endMinute", 0);

        if(startHour == 0 && startMinute == 0 && endHour == 0 && endMinute == 0){

            return;
        }

//        Calendar calStart = Calendar.getInstance();
////        if(calStart.get(Calendar.HOUR_OF_DAY) >= startHour && calStart.get(Calendar.MINUTE) >= startMinute){
////            calStart.add(Calendar.DAY_OF_YEAR, 1);
////        }
//        calStart.set(Calendar.HOUR_OF_DAY, startHour);
//        calStart.set(Calendar.MINUTE, startMinute);
//        calStart.set(Calendar.SECOND, 0);
//        calStart.set(Calendar.MILLISECOND, 0);
//        long timeStart = calStart.getTime().getTime();
//
//        Log.d("ShutdownService", "timeStart:" + startHour + ":" + startMinute);
//
//        Intent intentStart = new Intent(ShutdownService.this, AlarmReceiver.class);
//        intentStart.putExtra("action", "start2");
//        PendingIntent piStart = PendingIntent.getBroadcast(this,0, intentStart, 0);
//        am.set(AlarmManager.RTC_WAKEUP, timeStart, piStart);

        Calendar calEnd = Calendar.getInstance();
//        if(calEnd.get(Calendar.HOUR_OF_DAY) >= endHour && calEnd.get(Calendar.MINUTE) >= endMinute){
//            calEnd.add(Calendar.DAY_OF_YEAR, 1);
//        }
        calEnd.set(Calendar.HOUR_OF_DAY, endHour);
        calEnd.set(Calendar.MINUTE, endMinute);
        calEnd.set(Calendar.SECOND, 0);
        calEnd.set(Calendar.MILLISECOND, 0);
        long timeEnd = calEnd.getTime().getTime();

        Log.d("ShutdownService", "timeEnd:" + endHour + ":" + endMinute);
        Intent intentEnd = new Intent(ShutdownService.this, AlarmReceiver.class);
        intentEnd.putExtra("action", "end");
        PendingIntent piEnd = PendingIntent.getBroadcast(this,0, intentEnd, 0);
        am.set(AlarmManager.RTC_WAKEUP, timeEnd, piEnd);
    }
}
